package com.caifu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.SysMenuMapper;
import com.caifu.mapper.SysUserRoleMapper;
import com.caifu.pojo.SysMenu;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.SysUserRole;
import com.caifu.util.MenuUtils;
import com.caifu.util.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tww
 * ClassName: SysMenuService.java、
 * createTime: 2021年03月04日 17:45:33
 **/
@Service
public class SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;


    public List<SysMenu> getMenus(Integer id, SysUser user) {
        //查询用户角色
        List<SysUserRole> userRoles = sysUserRoleMapper.selectList(new QueryWrapper<SysUserRole>().eq("user_id", user.getId()));
        //判断当前用户角色是不是管理员
        if (UserUtils.isAdmin(userRoles)) {
            List<SysMenu> sysMenus = sysMenuMapper.selectList(new QueryWrapper<SysMenu>()
                    .ne("type", 4));
            return MenuUtils.getChildPerms(sysMenus, id);
        }
        List<SysMenu> sysMenus = sysMenuMapper.selectMenuListByUserId(user.getId());
        return MenuUtils.getChildPerms(sysMenus, id);
    }

    public List<SysMenu> getAllMenus() {
        List<SysMenu> sysMenus = sysMenuMapper.selectList(new QueryWrapper<SysMenu>()
                .isNull("is_admin")
                .or()
                .ne("is_admin", 1));
        return MenuUtils.getChildPerms(sysMenus, 0);
    }
}
